use std::sync::Arc;

use config::Config;

use crate::{common::chunking::*, index::{FingerPrintStoreSelector, IndexConfig}};

use super::{dedup::workflow::DedupWorkFlow, BackupResult};

pub struct SourcePool {
    config:Config,
    pub dedup_workflow:Option<Arc<DedupWorkFlow>>,
    pub chunking:ChunkingSelector,

    pub fp_store:FingerPrintStoreSelector,
}

impl SourcePool {
    pub fn from_config(config:Config) -> BackupResult<Self> {
        let chunking_builder:ChunkingBuilder = config.get("chunking").unwrap();

        let dedup_workflow = config
            .get::<bool>("dedup_in_workflow")
            .unwrap()
            .then_some(Arc::new(DedupWorkFlow::new()));
        
        let index_builder:IndexConfig = config.get("index").unwrap();
        let index_path:String = config.get("index_path")?;
        let index =index_builder.open(index_path)?;
        let fp_store = FingerPrintStoreSelector::new(&index)?;
        Ok(Self { 
            config, 
            dedup_workflow,
            chunking:chunking_builder.build(),
            fp_store,
        })
    }

    pub fn get_config(&self) -> &Config {
        &self.config
    }
}